package com.aaa.inbound.controller;


import com.aaa.entity.inbound.Warehousing;
import com.aaa.entity.inbound.WarehousingDetail;
import com.aaa.inbound.service.WarehousingDetailService;
import com.aaa.inbound.service.WarehousingService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/inbound/warehousing-detail")
@Api(description = "入库单详情管理")
public class WarehousingDetailController {
    @Autowired
    private WarehousingDetailService detailService;
    @Autowired
    private WarehousingService warehousingService;

    @PostMapping("/saveDetail")
    @ApiOperation(value = "根据接货单no新增商品")
    public R saveDetail(@RequestBody WarehousingDetail detail){
        Long commodityid = detail.getCommodityid();
        Long pid = detail.getPid();
        QueryWrapper<WarehousingDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        queryWrapper.eq("commodityid", commodityid);
        WarehousingDetail one = detailService.getOne(queryWrapper);
        if (one!=null){
            one.setQty(detail.getQty()+one.getQty());
            boolean b = detailService.updateById(one);
            if (b) {
                return R.ok().code(200).message("修改成功");
            }
            return R.ok().code(500).message("修改失败");
        }else {
            boolean b = detailService.saveDetail(detail);
            if (b) {
                return R.ok().code(200).message("添加成功");
            }
            return R.error().code(500).message("添加失败");
        }
    }

    @PostMapping("/selectDetailByNo/{no}")
    @ApiOperation(value = "根据no查询所有商品")
    public R selectDetailByNo(@PathVariable String no) {
        try {
            QueryWrapper<Warehousing> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("warehousingno", no);
            Warehousing one = warehousingService.getOne(queryWrapper);

            QueryWrapper<WarehousingDetail> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("pid", one.getId());
            List<WarehousingDetail> list = detailService.list(queryWrapper2);
            return R.ok().code(200).data("list", list);
        } catch (Exception e) {
            return R.error().code(500).message("查询方法有误");
        }
    }

}

